<?php
/**
 * 城市模型
 * User 1874
 */

namespace app\adminplatform\model;

use think\facade\Request;
use think\facade\View;
use think\Model;

class City extends Model {
    /**
     * 关联自身
     */
    public function city(){
        return $this->belongsTo(City::class, "ci_pid", "ci_id")->field("ci_id,ci_name");
    }
    /*
     * 城市列表分页
     * @param array $map 查询条件
     */
    public static function getCityPage(array $map=[]){

        return City::with("city")->where($map)->field('ci_id,ci_name,ci_pid')->paginate(["list_rows"=>PageNum,'query'=>Request::param()]);
    }
    /*
     * 获取不同城市类型列表
     * @param int $type 城市类型 0是国，1是省，2是市，3是区县，4镇，5街
     */
    public static function getCityTypeList(int $type){

        return City::where('ci_type', $type)->select();
    }
    /*
     * 查询单个城市信息
     * @param array $map 查询条件
     * @param int $type 查询类型
     * @return array
     */
    public static function getCityInfo(array $map=[], int $type=0){

        if($type == 0){
            return City::where($map)->find();
        }elseif($type == 1){
            return City::with("city")->where($map)->field('ci_id,ci_name,ci_pid')->find();
        }else{
            return City::where($map)->count("ci_id");
        }
    }
    /*
     * 根据城市id查找所有上级城市
     * @param int $cityId 城市id
     * @param string $filed 返回字段
     * @param int $returnType 返回类型 0返回省市县，1返回所有上级以数组形式
     * @return array
     */
    public static function getParentCityAll(int $cityId, string $filed='ci_name', int $returnType=0){
        if ($cityId == 0) return [];
        //获取城市父信息
        $city = City::where('ci_id', $cityId)->find();
        //查询上级城市信息
        $info = [$city['ci_type'] => $city];
        //循环查找上级城市
        for ($i = $city['ci_type']; $i > 1; $i--) {
            $info[$i-1] = self::getCityInfo(["ci_id"=>$info[$i]['ci_pid']]);
        }
        //返回省市县
        if ($returnType == 0){
            //创建一个空数组
            $cityInfo = [];
            //如果找到省份
            !empty($info[1]) ? $cityInfo['province'] = $info[1][$filed] : $cityInfo['province'] = null;
            //如果找到城市
            !empty($info[2]) ? $cityInfo['city'] = $info[2][$filed] : $cityInfo['city'] = null;
            //如果找到区县
            !empty($info[3]) ? $cityInfo['town'] = $info[3][$filed] : $cityInfo['town'] = null;

            return $cityInfo;
        }
        //直接返回全部数组
        return array_reverse($info);
    }
    /*
     * 查询所选城市下级id
     * @param int $parentId 城市父id
     * @param int $type 1获取其他数据，0只获取id
     * @return array
     */
    public static function getUnderCity(int $parentId, int $type=1){
        if ($parentId == 0) return (object)[];

        if($type == 1){
            $city = City::where('ci_pid',$parentId)->select();
        }else {
            //查询所选城市下级所有id
            $city = City::where('ci_pid', $parentId)->column('ci_id');
        }
        if(empty($city)) $city = (object)[];

        return $city;
    }
    /*
     * 省市县筛选条件
     */
    public static function cityScreening(){
        $map = [];
        //省份
        if (input('province') && !input('city') && !input('town')) {
            //找到省下面所属城市
            $cityIdsArr = City::getUnderCity(input('province'), 2);
            //逗号分隔条件
            $map[] = ['ci_pid', 'in', implode(',', $cityIdsArr)];
            View::assign('city', City::getUnderCity(input('province'))); //加载城市信息
        }
        //城市
        if (input('province') && input('city') && !input('town')) {
            $map[] = ['ad_cityId|ci_pid', '=', input('city')];
            View::assign('city', City::getUnderCity(input('province'))); //加载城市信息
            View::assign('town', City::getUnderCity(input('city'))); //加载区县信息
        }
        //区县
        if (input('province') && input('city') && input('town')) {
            $map[] = ['ci_id', 'in', input('town')];
            View::assign('city', City::getUnderCity(input('province'))); //加载城市信息
            View::assign('town', City::getUnderCity(input('city'))); //加载区县信息
        }

        return $map;
    }
}